var express = require("express");
var express = require("express");
var router = express.Router();
const shopsService = require("../service/shopsService.js");

// 查询所有服务
router.get("/all", async function (req, res) {
  let { type, value, current, size, typeId, valueId } = req.query;
  let searchObj = {};
  if (type) {// 使用正则可以实现模糊查询
    searchObj[type] = { $regex: value };
  }
  else if (typeId) {
    searchObj[typeId] = valueId;
  }
  let data = await shopsService.findAll(searchObj, {
    current: current || 1,
    size: size || 5,
  });
  res.send(data);
});

//增加服务
router.post("/", async function (req, res, next) {
  let data = await shopsService.insert(req.body);
  res.send(data);
});

//修改服务
router.put('/:id', async function (req, res) {
  // let obj = JSON.parse(req.body.dto)
  let id = req.params.id;
  let data = await shopsService.update(id, req.body);
  res.send(data);
})

//删除服务
router.delete("/:id", async function (req, res) {
  let id = req.params.id;
  let data = await shopsService.delete(id);
  res.send(data);
});
// 修改状态
router.put("/", async function (req, res) {
  let { id } = JSON.parse(req.query.params[0]);
  let { status } = JSON.parse(req.query.params[1]);
  await shopsService.upState(id, status);
});

module.exports = router;
